package com.escool.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.escool.model.TbGrado;
import com.escool.model.TbNivel;
import com.escool.model.TbRotaciones;
import com.escool.model.TbSedePersonalHasRol;

import com.escool.model.TbSedeHasPersonal;


import com.escool.service.CatEventoService;
import com.escool.service.EmpRolService;
import com.escool.service.GradosService;
import com.escool.service.NivelesService;
import com.escool.service.RolService;
import com.escool.service.RotacionesService;
import com.escool.service.SedeHasPersonalService;

@Controller
public class SedePersonalController {
	
	@Autowired
	EmpRolService EmpRolService;
	
	@Autowired
	SedeHasPersonalService SedePersonalService;

	
	@Autowired
	RotacionesService RotacionesService;
	
	@RequestMapping("showSedePersonal")
	public String showSedePersonal(Map<String, Object> map){
		map.put("SedePersonal", new TbSedeHasPersonal());
		map.put("TbSedeHasPersonalList", SedePersonalService.listVwPersonalSede());
		return "/vinSedePersonal";
	}
	
	@RequestMapping(value = "addSedePersonal", method = RequestMethod.POST)
	public String addRoles(@ModelAttribute("SedePersonal") @Valid TbSedeHasPersonal tbSedePersonal, BindingResult result){
		if (!result.hasErrors()) {
			SedePersonalService.addTbSedeHasPersonal(tbSedePersonal);
			return "redirect:/showSedePersonal";
		}
		return "";
	}
	
	@RequestMapping(value = "editSedePersonal", method = RequestMethod.POST)
	public String editRoles(@ModelAttribute("SedePersonal") @Valid TbSedeHasPersonal tbSedePersonal, BindingResult result){
		if (!result.hasErrors()) {
			SedePersonalService.editTbSedeHasPersonal(tbSedePersonal);
			return "redirect:/showSedePersonal";
		}
		return "";
	}
	
	@RequestMapping(value = "deleteSedePersonal", method = RequestMethod.POST)
	public String delRoles(@ModelAttribute("SedePersonal") @Valid TbSedeHasPersonal tbSedePersonal, BindingResult result){
		if (!result.hasErrors()) {
			SedePersonalService.delTbSedeHasPersonal(tbSedePersonal.getIdSedeHasPersonal());
			return "redirect:/showSedePersonal";
		}
		return "";
	}
	
	
	@RequestMapping("showEmpRolService_2")
	public String showEmpRolService(Map<String, Object> map,HttpServletRequest request,@ModelAttribute("SedePersonal") @Valid TbSedeHasPersonal tbSedePersonal, BindingResult result){
		
		TbSedePersonalHasRol tb = new TbSedePersonalHasRol();
		tb.setTbSedeHasPersonal(SedePersonalService.findTbSedeHasPersonal(tbSedePersonal.getIdSedeHasPersonal()));
		//tb.setTbSedeHasSeccion(VinSedeSeccionService.findTbSedeHasSeccion(Integer.parseInt(id)));
		map.put("tbEmpRolService", tb);
		map.put("TbSedePersonalHasRolList", EmpRolService.listTbSedePersonalHasRol(tb.getTbSedeHasPersonal().getIdSedeHasPersonal()));
		return "/empRolService";
	}
	
	@RequestMapping("showRotacionesService_2")
	public String showRotacionesService(Map<String, Object> map,HttpServletRequest request,@ModelAttribute("SedePersonal") @Valid TbSedeHasPersonal tbSedePersonal, BindingResult result){
		
		TbSedeHasPersonal per = SedePersonalService.findTbSedeHasPersonal(tbSedePersonal.getIdSedeHasPersonal());
		TbRotaciones tb = new TbRotaciones();
		tb.setCodigoEmp(per.getTbPersonal().getCodigoEmp());
		map.put("tbRotacionesService", tb);
		map.put("TbRotacionesList", RotacionesService.listVwRotaciones(tb.getCodigoEmp()));
		return "/rotaciones";
	}
	

	@RequestMapping("showRotacionesService_0")
	public String showRotacionesService0(Map<String, Object> map,HttpServletRequest request,@ModelAttribute("SedePersonal") @Valid TbSedeHasPersonal tbSedePersonal, BindingResult result){
		
		TbSedeHasPersonal per = SedePersonalService.findTbSedeHasPersonal(tbSedePersonal.getIdSedeHasPersonal());
		TbRotaciones tb = new TbRotaciones();
		tb.setCodigoEmp(per.getTbPersonal().getCodigoEmp());
		map.put("tbRotacionesService", tb);
		map.put("TbRotacionesList", RotacionesService.listVwRotaciones(tb.getCodigoEmp()));
		return "/showRotaciones";
	}
	
	@RequestMapping("showRotacionesService_1")
	public String showRotacionesService1(Map<String, Object> map,HttpServletRequest request,@ModelAttribute("SedePersonal") @Valid TbSedeHasPersonal tbSedePersonal, BindingResult result){
		
		TbSedeHasPersonal per = SedePersonalService.findTbSedeHasPersonal(tbSedePersonal.getIdSedeHasPersonal());
		TbRotaciones tb = new TbRotaciones();
		tb.setCodigoEmp(per.getTbPersonal().getCodigoEmp());
		map.put("tbRotacionesService", tb);
		map.put("TbRotacionesList", RotacionesService.listVwRotaciones(tb.getCodigoEmp()));
		return "/showRotacionesProf";
	}
}
